package com.spring.processor;

import com.spring.bean.Person;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

/**
 * @Author: tgy
 * @Date: 2020-08-03 11:39
 */

/**
 *
 * BeanPostProcessor
 * 作用 :   对spring创建的对象进行再加工，常见的有 AOP
 *
 * postProcessBeforeInitialization 在bean创建，设置property 之后执行
 *
 * postProcessAfterInitialization 是在 bean 使用 method-init 或者 InitialBean初始化之后，执行
 *
 */
public class CustomBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {

        if (bean instanceof Person){

            ((Person) bean).setAge(222);
            System.out.println(bean);
            return bean;
        }

        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }
}
